/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2024-08-08
 * Time: 21:00
 */
class Solution5 {
    public boolean isBalanced(TreeNode root) {
        if(root==null){
            return true;
        }
        int rootl=height(root.left);
        int rootr=height(root.right);
        if(Math.abs(rootl-rootr)>1){
            return false;
        }
        if(isBalanced(root.left)&&isBalanced(root.right)){
            return true;
        }
        return false;
    }
    public int height (TreeNode root){
        if(root==null){
            return 0;
        }
        return Math.max(height(root.left),height(root.right))+1;
    }
}
